package com.yunkss.eftool.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.yunkss.eftool.business.bean.UserBean;
import com.yunkss.eftool.business.dto.UserDTO;
import com.yunkss.eftool.business.query.UserQuery;
import com.yunkss.eftool.business.result.EfResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author csx
 * @Date 2024/4/20 16:52
 * @Desc CryptoController
 **/
@RestController
@RequestMapping("/eftool")
public class CryptoController {

    private final List<UserBean> userList = new ArrayList<>();

    private void init() {
        UserBean userBean = new UserBean();
        userBean.setAccount("efadmin");
        userBean.setPwd("123456");
        userBean.setNickName("管理员");
        userList.add(userBean);
        userList.add(new UserBean().setAccount("user").setPwd("123456").setNickName("用户"));
        userList.add(new UserBean().setAccount("user1").setPwd("123456").setNickName("用户1"));
    }

    @PostMapping("/login")
    public EfResult<UserDTO> login(@RequestBody UserQuery query) {
        if (userList.isEmpty()) {
            this.init();
        }
        List<UserBean> list = userList.stream().filter(userBean -> userBean.getAccount().equals(query.getAccount()) && userBean.getPwd().equals(query.getPwd())).collect(Collectors.toList());
        if (list.size() == 1) {
            UserDTO userDTO = new UserDTO();
            userDTO.setAccount(list.get(0).getAccount());
            userDTO.setNickName(list.get(0).getNickName());
            //模拟登录
            StpUtil.login(list.get(0).getId());
            //获取token
            String token = StpUtil.getTokenInfo().getTokenValue();
            userDTO.setToken(token);
            return EfResult.success("登录成功", userDTO);
        } else {
            return EfResult.fail("123456", "登录失败,账号密码不正确");
        }
    }

    @PostMapping("/post")
    public EfResult<JSONObject> post(@RequestBody Map<String, Object> param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(param);
        return EfResult.success("ef-post请求成功", jsonObject);
    }

    @PostMapping("/postFormData")
    public EfResult<JSONObject> postFormData(UserBean userBean) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userBean", userBean);
        return EfResult.success("ef-post-formData请求成功", jsonObject);
    }


    @DeleteMapping("/delete/{id}")
    public EfResult<JSONObject> delete(@PathVariable Integer id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        return EfResult.success("ef-delete请求成功", jsonObject);
    }


    @DeleteMapping("/deleteBody")
    public EfResult<JSONObject> deleteBody(@RequestBody Map<String, Object> param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(param);
        return EfResult.success("ef-delete-body请求成功", jsonObject);
    }

    @GetMapping("/get/{id}")
    public EfResult<JSONObject> find(@PathVariable Integer id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        return EfResult.success("ef-find请求成功", jsonObject);
    }

    @GetMapping("/getNew")
    public EfResult<JSONObject> get(@RequestParam(value = "key_csx") String id, @RequestParam(value = "key_cc") String str) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("str", str);
        return EfResult.success("ef-get-new请求成功", jsonObject);
    }

    @PutMapping("/put")
    public EfResult<JSONObject> update(@RequestBody Map<String, Object> param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(param);
        return EfResult.success("ef-put请求成功", jsonObject);
    }

    @PatchMapping("/patch/{id}")
    public EfResult<JSONObject> patch(@PathVariable Integer id, @RequestBody Map<String, Object> param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.putAll(param);
        return EfResult.success("ef-patch请求成功", jsonObject);
    }


}
